package com.dz.lm.service.reminder.impl;

import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.JPushUtils;
import com.dz.lm.dao.reminder.write.ReminderDao;
import com.dz.lm.service.AbstractService;
import com.dz.lm.service.reminder.ReminderService;
import com.google.common.collect.Maps;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Service
public class ReminderServiceImpl extends AbstractService implements ReminderService {

	public Logger logger = LogManager.getLogger(getClass());
	
    @Resource(name = "readReminderDao")
    private com.dz.lm.dao.reminder.read.ReminderDao readReminderDao;
	
    @Resource(name = "writeReminderDao")
    private ReminderDao writeReminderDao;
    
	@Resource(name="readLoginLogDao")
	private com.dz.lm.dao.log.read.LoginLogDao loginLogDao;
    
	public int addReminder(Map<String, Object> param) throws ServiceException {
		try {
			return writeReminderDao.addReminder(param);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return 0;
	}
    
    public List<Map<String, Object>> qryReminderInfo(Map<String, Object> param) throws ServiceException {
    	List<Map<String, Object>> reminderList = null;
    	List<Map<String, Object>> reminderDetailList = null;
    	try {
    		reminderList = readReminderDao.qryReminderList(param);
    		for (Map<String, Object> reminder : reminderList) {
    			param.put("type", reminder.get("type"));
    			if ("1".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("managerList", reminderDetailList);
    			} else if ("2".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("agentList", reminderDetailList);
    			} else if ("3".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("agentList", reminderDetailList);
    			} else if ("4".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("applyList", reminderDetailList);
    			} else if ("5".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("applyList", reminderDetailList);
    			} else if ("6".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("productList", reminderDetailList);
    			} else if ("7".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("productList", reminderDetailList);
    			} else if ("8".equals(reminder.get("type"))) {
    	    		reminderDetailList = readReminderDao.qryReminderDetailList(param);
    	    		reminder.put("applyList", reminderDetailList);
    			}
    		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return reminderList;
    }
    
    public int clickReminderInfo(Map<String, Object> param) throws ServiceException {
    	int count = 0;
    	try {
    		count = writeReminderDao.delReminder(param);
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return count;
    }
    
    /**
     * 消息推送
     * 
     * @return
     * @throws Exception
     */
    @Override
    public void sendMessagePush(Map<String, Object> param) throws ServiceException {
    	Runnable runable = new AsyncThread(param);
    	runable.run();
    }
    
	/**
	 * 消息推送
	 */
	private void pushMessage(Map<String, Object> param) throws ServiceException {
		Map<String, Object> pushInfo = null;
		Map<String, String> map_jp = Maps.newHashMap();
		try {
			pushInfo = loginLogDao.qryPushIdByUserId(param);
			if (pushInfo == null || pushInfo.isEmpty() || pushInfo.get("pushId") == null || "".equals(pushInfo.get("pushId"))) {
				return;
			}
            String pushTitle = param.get("pushTitle").toString();
            String pushContent = param.get("pushContent").toString();
            // 推送
            if ("Android".equals(param.get("platform"))) {
            	// jpush
                String deviceAndro = pushInfo.get("pushId").toString();
                //push消息
//                JPushUtils.pushAndroidMessageByRegistrationID(pushTitle, pushContent, deviceAndro);
                JPushUtils.pushAndroidNotificationByRegistrationID(pushTitle, pushContent, new HashMap(),deviceAndro);
            } else if ("IOS".equals(param.get("platform"))){
            	String[] deviceIos = param.get("IOS").toString().split(",");
            	JPushUtils.pushIosNotificationByRegistrationID(pushContent, map_jp, deviceIos);
            } else {
            	//Nothing
            }
            logger.info("消息推送成功");
		}
		catch (DaoException e) {
			logger.info("消息推送失败");
			throw new ServiceException(e.getCause());
		}
    }
    
	public class AsyncThread implements Runnable {
		private Map<String, Object> param;

		AsyncThread(Map<String, Object> param) {
			this.param = param;
		}
		@Override
		public void run() {
			try {
				pushMessage(param);
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
	}
}
